# Definition for a binary tree node.
class TreeNode(object):
  def __init__(self, x):
    self.val = x
    self.left = None
    self.right = None


class Codec:
  def serialize(self, root) -> str:
    qwq, res = [root], []
    if not root:
      return '[]'
    while qwq:
      node = qwq.pop(0)
      if node:
        res.append(str(node.val))
        qwq.append(node.left)
        qwq.append(node.right)
      else:
        res.append('null')
    return '[' + ','.join(res) + ']'
    

  def deserialize(self, data) -> TreeNode:
    if data == '[]': return
    vals, i = data[1:-1].split(','), 1  # 去除中括号
    root = TreeNode(int(vals[0]))
    qwq = [root]
    while qwq:
      node = qwq.pop(0)
      if vals[i] != 'null':
        node.left = TreeNode(int(vals[i]))
        qwq.append(node.left)
      i = i + 1
      if vals[i] != 'null':
        node.right = TreeNode(int(vals[i]))
        qwq.append(node.right)
      i = i + 1
    return root


'''
STATUS: LEARNT
TRIES:  1
TIME:   160 MS
MEMORY: 18.7 MB
'''
if __name__ == '__main__':
  codec = Codec()
  # 测试序列化
  root = TreeNode(1)
  root.left = TreeNode(2)
  root.right = TreeNode(3)
  root.right.left = TreeNode(4)
  root.right.right = TreeNode(5)
  print(codec.serialize(root))
